Spot-On
spot-on-mceliece.h
1 /*
2 ** Copyright (c) 2011 - 10^10^10, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Software based on specifications provided by Antoon Bosselaers,
6 ** RenĂ© Govaerts, Robert McEliece, Bart Preneel, Marek Repka,
7 ** Christopher Roering, Joos Vandewalle.
8 **
9 ** Redistribution and use in source and binary forms, with or without
10 ** modification, are permitted provided that the following conditions
11 ** are met:
12 ** 1. Redistributions of source code must retain the above copyright
13 ** notice, this list of conditions and the following disclaimer.
14 ** 2. Redistributions in binary form must reproduce the above copyright
15 ** notice, this list of conditions and the following disclaimer in the
16 ** documentation and/or other materials provided with the distribution.
17 ** 3. The name of the author may not be used to endorse or promote products
18 ** derived from skein without specific prior written permission.
19 **
20 ** SPOT-ON IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 ** SPOT-ON, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 
32 #ifndef _spoton_mceliece_h_
33 #define _spoton_mceliece_h_
34 
35 #ifdef SPOTON_MCELIECE_ENABLED
36 #include <NTL/GF2E.h>
37 #include <NTL/GF2EX.h>
38 #include <NTL/GF2EXFactoring.h>
39 #include <NTL/GF2X.h>
40 #include <NTL/GF2XFactoring.h>
41 #include <NTL/mat_GF2.h>
42 #include <NTL/mat_ZZ_p.h>
43 #include <NTL/vec_GF2E.h>
44 
45 #include <limits>
46 #include <sstream>
47 #include <vector>
48 
49 class spoton_mceliece_private_key
50 {
51  public:
52  spoton_mceliece_private_key(const char *privateKey,
53  const size_t privateKeyLength);
54  spoton_mceliece_private_key(const size_t m, const size_t t);
55  ~spoton_mceliece_private_key();
56 
57  NTL::GF2EX X(void) const
58  {
59  return m_X;
60  }
61 
62  NTL::GF2EX gZ(void) const
63  {
64  return m_gZ;
65  }
66 
67  NTL::mat_GF2 G(void) const
68  {
69  return m_G;
70  }
71 
72  NTL::mat_GF2 P(void) const
73  {
74  return m_P;
75  }
76 
77  NTL::mat_GF2 Pinv(void) const
78  {
79  return m_Pinv;
80  }
81 
82  NTL::mat_GF2 S(void) const
83  {
84  return m_S;
85  }
86 
87  NTL::mat_GF2 Sinv(void) const
88  {
89  return m_Sinv;
90  }
91 
92  NTL::vec_GF2E L(void) const
93  {
94  return m_L;
95  }
96 
97  QByteArray conversion(void) const
98  {
99  return m_conversion;
100  }
101 
102  bool ok(void) const
103  {
104  return m_ok;
105  }
106 
107  bool prepareG(const NTL::mat_GF2 &R);
108 
109  size_t k(void) const
110  {
111  return m_k;
112  }
113 
114  size_t m(void) const
115  {
116  return m_m;
117  }
118 
119  size_t n(void) const
120  {
121  return m_n;
122  }
123 
124  size_t preSynTabSize(void) const
125  {
126  return m_preSynTab.size();
127  }
128 
129  size_t t(void) const
130  {
131  return m_t;
132  }
133 
134  std::vector<NTL::GF2EX> preSynTab(void) const
135  {
136  return m_preSynTab;
137  }
138 
139  std::vector<long int> swappingColumns(void) const
140  {
141  return m_swappingColumns;
142  }
143 
144  void swapSwappingColumns(const long int i, const long int j);
145 
146  private:
147  NTL::GF2EX m_X;
148  NTL::GF2EX m_gZ;
149  NTL::mat_GF2 m_G;
150  NTL::mat_GF2 m_P;
151  NTL::mat_GF2 m_Pinv;
152  NTL::mat_GF2 m_S;
153  NTL::mat_GF2 m_Sinv;
154  NTL::vec_GF2E m_L;
155  QByteArray m_conversion;
156  bool m_ok;
157  size_t m_k;
158  size_t m_m;
159  size_t m_n;
160  size_t m_t;
161  std::vector<NTL::GF2EX> m_preSynTab;
162  std::vector<long int> m_swappingColumns;
163  bool prepareP(void);
164  bool preparePreSynTab(void);
165  bool prepareS(void);
166  bool prepare_gZ(void);
167  void prepareSwappingColumns(void);
168  void reset(const bool ok);
169 };
170 
171 class spoton_mceliece_public_key
172 {
173  public:
174  spoton_mceliece_public_key(const size_t m, const size_t t);
175  spoton_mceliece_public_key(const size_t t, const NTL::mat_GF2 &Gcar);
176  ~spoton_mceliece_public_key();
177 
178  NTL::mat_GF2 Gcar(void) const
179  {
180  return m_Gcar;
181  }
182 
183  bool ok(void) const
184  {
185  return m_ok;
186  }
187 
188  bool prepareGcar(const NTL::mat_GF2 &G,
189  const NTL::mat_GF2 &P,
190  const NTL::mat_GF2 &S);
191 
192  size_t k(void) const
193  {
194  return static_cast<size_t> (m_Gcar.NumRows());
195  }
196 
197  size_t n(void) const
198  {
199  return static_cast<size_t> (m_Gcar.NumCols());
200  }
201 
202  size_t t(void) const
203  {
204  return m_t;
205  }
206 
207  private:
208  NTL::mat_GF2 m_Gcar;
209  bool m_ok;
210  size_t m_t;
211  void reset(const bool ok);
212 };
213 
214 class spoton_mceliece
215 {
216  public:
217  explicit spoton_mceliece(const QByteArray &pk);
218  spoton_mceliece(const char *privateKey,
219  const size_t privateKeyLength,
220  const QByteArray &publicKey);
221  spoton_mceliece(const QByteArray &conversion,
222  const size_t m,
223  const size_t t);
224  ~spoton_mceliece();
225  bool decrypt(const std::stringstream &ciphertext,
226  std::stringstream &plaintext) const;
227  bool encrypt(const char *plaintext,
228  const size_t plaintext_size,
229  std::stringstream &ciphertext) const;
230  bool generatePrivatePublicKeys(void);
231 
232  bool ok(void) const
233  {
234  if(m_privateKey && m_publicKey)
235  return m_privateKey->ok() && m_publicKey->ok();
236  else
237  return false;
238  }
239 
240  size_t m(void) const
241  {
242  return m_m;
243  }
244 
245  size_t t(void) const
246  {
247  return m_t;
248  }
249 
250  static double expansion(const size_t m, const size_t t)
251  {
252  size_t k = 0;
253  size_t n = 1 << m; // 2^m
254 
255  if(m * t < n)
256  k = n - m * t;
257 
258  if(k > 0)
259  return static_cast<double> (n) / static_cast<double> (k);
260  else
261  return 0.0;
262  }
263 
264  void initializeSystemParameters(const size_t m, const size_t t)
265  {
266  m_m = m;
267  m_n = 1 << m_m; // 2^m
268  m_t = t;
269 
270  /*
271  ** Some calculations.
272  */
273 
274  if(m_m * m_t < m_n)
275  m_k = m_n - m_m * m_t;
276  else
277  m_k = 0;
278  }
279 
280  void privateKeyParameters(QByteArray &privateKey) const;
281  void publicKeyParameters(QByteArray &publicKey) const;
282 
283  private:
284  QByteArray m_conversion; /*
285  ** 000 - None
286  ** foa - Fujisaki-Okamoto Model A
287  ** fob - Fujisaki-Okamoto Model B
288  */
289  spoton_mceliece_private_key *m_privateKey;
290  spoton_mceliece_public_key *m_publicKey;
291  size_t m_k;
292  size_t m_m;
293  size_t m_n;
294  size_t m_t;
295 };
296 
297 #endif
298 #endif